#include <QDirModel>
#include <QDebug>

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow* MainWindow::mainWindow = NULL;

MainWindow* MainWindow::instance()
{
	return mainWindow;
}

MainWindow::MainWindow(Splash& splash, QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow), about(NULL)
{
    selectedView = 3;
    ui->setupUi(this);
    setCentralWidget(ui->gridLayoutWidget);
    ui->gridLayout->setMargin(0);
    ui->gridLayout->setSpacing(2);
    ui->gridLayoutWidget->setStyleSheet(".QWidget { background-color: black; } QToolBar { min-height: 18px; max-height: 18px; border: 1px solid black; border-left: 0px; margin: 0px; padding: 0px; } ");
    ui->actionPanels->setMenu(createPopupMenu());
    for (int i = 0; i < 4; ++i)
    {
        splash.setLoadStep(3 + i);
        modelViews[i] = new ModelView(ui->gridLayoutWidget, i * 2);
        ui->gridLayout->addWidget(modelViews[i], i % 2, i / 2, 1, 1);
    }
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 4; ++j)
        {
            if (i != j)
            {
                QObject::connect(modelViews[i], SIGNAL(selected()), modelViews[j], SLOT(unselect()));
            }
        }
        QObject::connect(modelViews[i], SIGNAL(selected()), SLOT(updateSelectedView()));
        QObject::connect(modelViews[i]->getToolBar(), SIGNAL(doubleClick()), SLOT(switchGridView()));
    }
    modelViews[selectedView]->select();
    splash.setLoadStep(7);
    treeLayout = new QGridLayout(ui->dockTreeContent);
    treeLayout->setSpacing(0);
    treeLayout->setMargin(0);
    ui->dockTreeContent->setLayout(treeLayout);
    QDirModel *model = new QDirModel;
    treeView = new QTreeView(ui->dockTreeContent);
    treeView->setHeaderHidden(true);
    treeView->setModel(model);
    treeView->hideColumn(1);
    treeView->hideColumn(2);
    treeView->hideColumn(3);
    treeLayout->addWidget(treeView, 0, 0, 1, 1);
    treeView->show();
    splash.setLoadStep(8);
    toolLayout = new QGridLayout(ui->dockToolsContent);
    toolLayout->setSpacing(0);
    toolLayout->setMargin(0);
    ui->dockToolsContent->setLayout(toolLayout);
    toolTab = new QTabWidget();
    toolLayout->addWidget(toolTab, 0, 0, 1, 1);
    toolTab->show();
    layout = saveState();
    splash.setLoadStep(9);
    about = new About(this);
    help = new HelpBrowser(this);
    QObject::connect(ui->actionAbout, SIGNAL(triggered()), SLOT(showAbout()));
    QObject::connect(ui->actionHelp, SIGNAL(triggered()), SLOT(showHelp()));
    QObject::connect(ui->actionSwitch_canva_mode, SIGNAL(triggered()), SLOT(switchGridView()));
    QObject::connect(ui->actionReset_workspace, SIGNAL(triggered()), SLOT(resetLayout()));
    QObject::connect(ui->actionZoom, SIGNAL(toggled(bool)), SLOT(changeMouseMode(bool)));
    QObject::connect(ui->actionTranslate_View, SIGNAL(toggled(bool)), SLOT(changeMouseMode(bool)));
    QObject::connect(ui->actionRotate_View, SIGNAL(toggled(bool)), SLOT(changeMouseMode(bool)));
    ui->actionRotate_View->setChecked(true);
	mainWindow = this;
}

MainWindow::~MainWindow()
{
    delete treeView;
    delete treeLayout;
    delete ui;
    delete about;
    delete help;
    for (int i = 0; i < 4; ++i)
        delete modelViews[i];
}

void    MainWindow::resetLayout()
{
    restoreState(layout);
}

void    MainWindow::switchGridView()
{
    if (ui->gridLayout->count() == 4)
    {
        for (int i = 0; i < 4; ++i)
        {
            ui->gridLayout->removeWidget(modelViews[i]);
            modelViews[i]->hide();
        }
        ui->gridLayout->addWidget(modelViews[selectedView], 0, 0, 1, 1);
        modelViews[selectedView]->show();
    }
    else
    {
        ui->gridLayout->removeWidget(modelViews[selectedView]);
        for (int i = 0; i < 4; ++i)
        {
            ui->gridLayout->addWidget(modelViews[i], i % 2, i / 2, 1, 1);
            modelViews[i]->show();
        }
    }
}

void    MainWindow::showAbout()
{
    about->exec();
}

void    MainWindow::showHelp()
{
    help->show();
}

void    MainWindow::closeEvent(QCloseEvent *event)
{
    if (help->isVisible())
        help->close();
}

void    MainWindow::updateSelectedView()
{
    for (int i = 0; i < 4; ++i)
    {
        if (modelViews[i]->isSelected())
        {
            selectedView = i;
            break;
        }
    }
}

void    MainWindow::changeMouseMode(bool enable)
{
    if (enable == true)
    {
        if (QObject::sender() == ui->actionZoom)
        {
            mouseMode = ZOOM;
            ui->actionTranslate_View->setChecked(false);
            ui->actionRotate_View->setChecked(false);
        }
        else if (QObject::sender() == ui->actionTranslate_View)
        {
            mouseMode = TRANSLATE;
            ui->actionZoom->setChecked(false);
            ui->actionRotate_View->setChecked(false);
        }
        if (QObject::sender() == ui->actionRotate_View)
        {
            mouseMode = ROTATE;
            ui->actionZoom->setChecked(false);
            ui->actionTranslate_View->setChecked(false);
        }
    }
    if (!ui->actionZoom->isChecked() && !ui->actionTranslate_View->isChecked() && !ui->actionRotate_View->isChecked())
    {
        mouseMode = SELECT;
    }
}

